package com.trafficradio.trafficradio.network;

import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;

import com.trafficradio.trafficradio.util.Data;
import com.trafficradio.trafficradio.operation.Operation;

/**
 * Created by jwzxg on 2016/7/8.
 */
public class LocationUpdate implements LocationListener{

    private static final int MINIMUM_TIME = 5000;  // 5s
    private static final int MINIMUM_DISTANCE = 10; // 10m
    private LocationManager mLocationManager;
    private Context context;
    public LocationUpdate(Context context){
        this.context = context;
        mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINIMUM_TIME, MINIMUM_DISTANCE,this);
        }

    }
    @Override
    public void onLocationChanged(Location location) {
        update(location);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
    public void update(Location location){
        Operation.updateLocation(location);
        Data.setLocation(location);
    }
}
